package cn.edu.neu.studentmsg_java.controller.workbench;

import cn.edu.neu.studentmsg_java.cons.R;
import cn.edu.neu.studentmsg_java.cons.State;
import cn.edu.neu.studentmsg_java.model.TR;
import cn.edu.neu.studentmsg_java.model.Teacher;
import cn.edu.neu.studentmsg_java.service.TRService;
import cn.edu.neu.studentmsg_java.service.TeacherService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

//教师控制层
@RestController
@RequestMapping("/workbench/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private TRService trService;

    //获取教师级别名称数据
    @RequestMapping("/getData")
    public R getData(){
        List<Object> list = new ArrayList<>();
        //获取教师级别信息
        List<TR> trList = trService.findAll();
        //获取教师级别名称
        List<String> RankNameList = trService.findRankName();
        list.add(trList);
        list.add(RankNameList);
        if (ObjectUtils.isNotEmpty(list)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(list).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //根据任意条件进行教师查询
    @RequestMapping("/findTeacherPart")
    public R findTeacherPart(Teacher teacher) {
        //动态查询查询教师信息
        List<Teacher> teacherList = teacherService.findTeacherPart(teacher);
        if (ObjectUtils.isNotEmpty(teacherList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(teacherList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //通过教师编号对教师进行删除
    @RequestMapping("/removeTeacherById")
    public R removeTeacherById(Integer teacher_id) {
        //通过teacher_id删除教师信息
        int result = teacherService.removeTeacherById(teacher_id);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_DELETE_ERROR.getCode()).msg(State.DB_DELETE_ERROR.getMsg()).build();
    }

    //添加教师信息
    @RequestMapping("/addTeacher")
    public R addTeacher(@RequestBody Teacher teacher) {
        //根据接受的数据添加教师信息
        int result = teacherService.addTeacher(teacher);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_SAVE_ERROR.getCode()).msg(State.DB_SAVE_ERROR.getMsg()).build();
    }

    //通过teacher_id获取教师信息
    @RequestMapping("/toModifyTeacherPage")
    public R toModifyTeacherPage(Integer teacher_id) {
        //通过教师id查询教师信息（用于跳转修改页面时得到要修改教师的信息）
        Teacher teacher = teacherService.findTeacherByTeacherId(teacher_id);
        if (ObjectUtils.isNotEmpty(teacher)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(teacher).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //通过教师编号修改教师信息
    @RequestMapping("/modifyTeacherById")
    public R modifyTeacherById(@RequestBody Teacher teacher) {
        //通过教师id去动态的修改教师信息
        int result = teacherService.modifyTeacherById(teacher);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_UPDATE_ERROR.getCode()).msg(State.DB_UPDATE_ERROR.getMsg()).build();
    }
}
